package jscinttools.transmission;

import jscinttools.utilities.aidautilities.AidaFactories;
import jscinttools.utilities.aidautilities.AidaFileReader;
import hep.aida.ITree;
import hep.aida.ITreeFactory;
import hep.aida.ITuple;
import hep.aida.ITupleFactory;
import hep.aida.IDataPointSetFactory;
import hep.aida.IDataPointSet;
import java.lang.System;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.Double;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import fr.in2p3.ipnl.aidautilities.TupleReader;
import fr.in2p3.ipnl.utilities.fileutilities.HeaderDecapitator;
import jscinttools.transmission.*;

/**
 *  Generates the Rates calculation
 *
 *@author     Marc-Antoine Verdier
 *@created    19 mai 2011
 */
public class TransmissionAnalysisTDC implements AidaFactories
{
	private String path;
	private String files;
	private String outPath;
	private int parts;


	/**
	 *  Constructor for the TransmissionAnalysisTDC object
	 *
	 *@param  path     Description of the Parameter
	 *@param  files    Description of the Parameter
	 *@param  outPath  Description of the Parameter
	 *@param  parts    Description of the Parameter
	 */
	public TransmissionAnalysisTDC(String path, String files, String outPath, int parts)
	{
		this.path = path;
		this.files = files;
		this.outPath = outPath;
		this.parts = parts;
	}


	/**
	 *  Constructor for the TransmissionAnalysisTDC object
	 *
	 *@param  path   Description of the Parameter
	 *@param  files  Description of the Parameter
	 *@param  parts  Description of the Parameter
	 */
	public TransmissionAnalysisTDC(String path, String files, int parts)
	{
		this(path, files, path, parts);
	}


	///**
	// *  Constructor for the TransmissionAnalysisTDC object
	// */
	//public TransmissionAnalysisTDC(String fullFileNames, int parts)
	//{
	//	this(findPath(fullFileNames), findFileName(fullFileNames), findPath(fullFileNames), parts);
	//}
	//
	//
	//private String findPath(String fullFileNames)
	//{
	//	String[] partString = fullFileNames.split(File.separator);
	//	StringBuilder sb = new StringBuilder();
	//	for (int i = 0; i < partString.length - 1; i++)
	//	{
	//		sb.append(partString[i] + File.separator);
	//	}
	//	return sb.toString();
	//}
	//
	//
	//private String findFileName(String fullFileNames)
	//{
	//	String[] partString = fullFileNames.split(File.separator);
	//	int index = partString.length - 1;
	//	return partString[index];
	//}


	/**
	 *  Do the transmission TDC analysis for a given set of Part files. An Aida
	 *  file will be made for each Part plus a Aida file from the concatenated Part
	 *  Aida file is made
	 */
	public void doAnalysisTDC()
	{
		try
		{
			try
			{
				for (int i = 0; i < parts; i++)
				{
					String inFile = files + "_Part_" + i + ".txt";
					String outFile = files + "_Part_" + i + "_NoH.txt";
					String eventFile = files + "_Part_" + i + "_events";
					HeaderDecapitator hd = new HeaderDecapitator(path, inFile, outFile, 4);
					EventLED evtLED = new EventLED(path, outFile);
					evtLED.makeAidaTDC(eventFile);
				}

				String tupName = files + "_events";
				ITree tree = AidaFactories.TREE_FACTORY.createTree(outPath + File.separator + tupName + ".aida", "xml", ITreeFactory.CREATE);

				String tupCols = "int event, int ol, int ch1, int ch2, int ch3, int ch4, int ch5, int ch6, int ch7";
				ITuple eventTuple = AidaFactories.ANALYSIS_FACTORY.createTupleFactory(tree).create(tupName, tupName, tupCols);

				eventTuple.start();
				for (int i = 0; i < parts; i++)
				{
					String eventFile = files + "_Part_" + i + "_events";
					AidaFileReader afr = new AidaFileReader(path, eventFile);
					ITuple tup = afr.grabTuple(eventFile);
					tup.start();
					while (tup.next())
					{
						eventTuple.next();
						for (int k = 0; k < tup.columns(); k++)
						{
							eventTuple.fill(k, tup.getInt(k));
						}
						eventTuple.addRow();
					}
				}

				tree.commit();
				System.out.println("Writting file : " + outPath + File.separator + eventTuple.title());
			} catch (FileNotFoundException fnfe)
			{
				System.out.println(fnfe);
			}
		} catch (IOException ioe)
		{
			System.out.println("Problem with aida file: " + ioe);
		}
	}
}

